/*
 * This file is part of algowala.
 *
 * algowala is copyright (c) 2012 Jagannathan Sampath <algowala@jagernot.com>
 *
 * algowala is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * algowala is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with algowala.  If not, see <http://www.gnu.org/licenses/>.
 *
*/
#ifndef __aw_thread
#define __aw_thread

#include <pthread.h>
#include <list>
#include <vector>
#include "stock.h"

struct aw_thread { // algowala thread
  
  pthread_t th; // pthread info
  
  std::list<stock> stocks; // list of stocks ticking on this thread
  int num_stocks; // number of stocks ticking on this thread
  
  // operations
  //
  enum {quit = 0, tick, add, remove, get, edit, draw};
  static int opcode; // current opcode

  int checked; // was this thread checked by an operation? 

  // pthread callback; arg is -> to aw_thread
  static void* run (void* arg);
  
  // for opcode = add
  static stock added_stock; // the added stock
  static pthread_t* add_th; // the thread the stock will be added to
  static int added; // added stock?
  static size_t add_rr; // id by round robin
  static void add_stock (std::vector<aw_thread>& v, const stock& s);
  
  // for opcode = draw
  static int ok_draw;
  
  // for opcode = remove
  static std::string remove_sym; // stock symbol
  
  // for opcode = edit
  enum {sym = 1, price = 1 << 1, volatility = 1 << 2, volume = 1 << 3, volume_volatility = 1 << 4, n_times = 1 << 5, m_secs = 1 << 6};
  static std::string edit_sym; // stock symbol
  static int edit_what; // stock symbol/company, price, volatility, n_times & m_secs  
  static stock edit_stock; // holds the changed params to copy into edited stock
  
  // for opcode = get
  static std::string get_sym; // stock symbol
  static std::string get_result; // stock parameters as TCL friendly list with format: symbol company price volatility volume volume_volatility n_times m_seconds
  
  aw_thread () : checked (0) {}
  
};

typedef std::list<stock>::iterator stock_iterator;

#endif
